package com.fly.consumers.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fly.consumers.feign.HelloFeign;
import com.fly.consumers.service.SkillService;

@RestController
public class HelloController {

	public static boolean hystrixFlag = false;
	public static boolean timeoutFlag = false;
	
	@Autowired
	DiscoveryClient client;
	
	@Autowired
	HelloFeign feign;
	
	@Autowired
	SkillService skillService;
	
	@GetMapping("/hello")
	public String hello() {
		System.err.println("feign");
		List<String> services = client.getServices();
		for (String sv : services) {
			System.err.println("----------------------");
			System.err.println(sv);
			List<ServiceInstance> inss = client.getInstances(sv);
			for (ServiceInstance serviceInstance : inss) {
				System.err.println(serviceInstance.getHost()+":"+serviceInstance.getPort());
			}
			System.err.println("----------------------");
		}
		HashMap<String, String> param = new HashMap<String, String>();
		param.put("name", "test hash map param");
		param.put("age", "test hash map param");
		param.put("sex", "test hash map param");
		String result = feign.hello();
		String result2 = feign.hello2(param);
		System.err.println(result);
		System.err.println(result2);
		return result;
	}
	
	@GetMapping("/toggleError")
	public String toggleError() {
		hystrixFlag = !hystrixFlag;
		return "open hystrix flag : "+Boolean.toString(hystrixFlag);
	}
	
	@GetMapping("/toggleTimeout")
	public String toggleTimeout() {
		timeoutFlag = !timeoutFlag;
		return "open timeout flag : "+Boolean.toString(timeoutFlag);
	}
	
	@GetMapping("/skill")
	public String hystrix() {
		System.err.println("start time:"+new Date().toString());
		String result = skillService.getSkill("param1");
		skillService.getSkill2("param2");
		System.err.println("end time:"+new Date().toString());
		String threadName = "(" + Thread.currentThread().getName() + ")";
		String now = new Date().toString();
		String log = "result = " + threadName + " : " + now + " : " + result;
		System.err.println(log);
		if ("error".equals(result)) {
			throw new RuntimeException("skill service fallback.");
		}
		return log;
	}
	
	@ExceptionHandler(value = RuntimeException.class)
	@ResponseBody
	public ResponseEntity<?> errorHandle(RuntimeException err) {
		ResponseEntity<String> resp = new ResponseEntity<String>("error handle.", HttpStatus.BAD_REQUEST);
		return resp;
	}
}
